<?php
/**
 * @package: Asalsuri
 * @subpackage: Club image Model
 * @author:  Pham Van Diep
 * @date: 19/2/1024
 * 
*/
class Image_Model extends CI_Model{
	var $database;
	public function __construct() {
        parent::__construct();
		$this->database = $this->db->conn_id;
		
	}
    /**
     * @author: Pham Van Diep
     * @description: create object image
     * @param array $row: array image
     * @return object | null
    */
	private function _imageToObj($row) {
		if(isset($row)) {
			$objImage			    = new stdClass();
			$objImage->id 	    	= isset($row['id'])          ? $row['id']          : 0;
            $objImage->clubid 		= isset($row['clubid'])      ? $row['clubid']      : 0;
            $objImage->srcid 		= isset($row['srcid'])       ? $row['srcid']       : 0;
            $objImage->type 		= isset($row['type'])        ? $row['type']        : 0;
			$objImage->name         = isset($row['name'])        ? $row['name']        : '';
			$objImage->tag          = isset($row['tag'])         ? $row['tag']         : '';
			$objImage->short 	    = isset($row['short'])       ? $row['short']       : '';
			$objImage->description	= isset($row['description']) ? $row['description'] : '';
            $objImage->path	        = isset($row['path'])        ? $row['path']        : '';
            $objImage->image    	= isset($row['image'])       ? $row['image']       : '';
            $objImage->format	    = isset($row['format'])      ? $row['format']      : '';
            $objImage->width        = isset($row['width'])       ? $row['width']       : 0;
            $objImage->height       = isset($row['height'])      ? $row['height']      : 0;
            $objImage->status       = isset($row['status'])      ? $row['status']      : 0;
            $objImage->created	    = isset($row['created'])     ? $row['created']     : '';
            $objImage->modified	    = isset($row['modified'])    ? $row['modified']    : '';
            $objImage->logid	    = isset($row['logid'])       ? $row['logid']       : 0;
			return $objImage;
		}
		return null;
	}
    /**
     * @author: Pham Van Diep
     * @description: get image info
     * @param integer $intId: ID
     * @return object | null
    */
	public function get($intId) {	
		$stmt = $this->database->prepare('CALL clubs_img_get(?)');
		$stmt->bindParam(1, $intId, PDO::PARAM_INT, 11);
		$stmt->execute();
		return  $this->_imageToObj($stmt->fetch());				
	}
    
    /**
    * @author: Pham Van Diep
    * @description: get all club image
    * @param integer $intLoginid: user login id
    * @param integer $intStatus:
    *                           0: inactive
    *                           1: active
    *                           2: suspend report
    *                           3: rejected
    *                           4: member delete
    *                           5: leader delete 
    *                           6: admin delete
    * @param integer $intClubid: club id
    * @param datetime $dStart : get from start date
    * @param datetime $dEnd : get to date 
    * @param integer $intOffset: row start paging 
    * @param integer $intLimit: display row on paging    
    * @param array
    */    
    public function getAll($intLoginid,$intStatus,$intClubid,$dStart,$dEnd,$intOffset,$intLimit,&$intTotal=0) {
		$stmt = $this->database->prepare('CALL clubs_img_getall_a(?,?,?,?,?,?,?)');
		$stmt->bindParam(1, $intLoginid,     PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $intStatus,      PDO::PARAM_INT, 4);
		$stmt->bindParam(3, $intClubid,      PDO::PARAM_INT, 11);
        $stmt->bindParam(4, $dStart,         PDO::PARAM_STR);
        $stmt->bindParam(5, $dEnd,           PDO::PARAM_STR);
        $stmt->bindParam(6, $intOffset,      PDO::PARAM_INT, 11);
        $stmt->bindParam(7, $intLimit,       PDO::PARAM_INT, 6);		
		$stmt->execute();
		$result = $stmt->fetchAll();        
		$intTotal = isset($result[0]['foundRows']) ? $result[0]['foundRows'] : 0;
		return isset($result) ? $result : null;
	}
		/**
     * @author: Pham Van Diep
	 * @description: add club image info  
	 * @param interger $intLoginId: id admin loggin (as root id width insert permission)
	 * @param integer  $intSrcId: referal id
	 * @param integer  $intType: image type
	 * @param string   $strName: name
	 * @param string   $strTag:  tag
     * @param string   $strShort:  short description
     * @param string   $strDescription:  image description
     * @param string   $strPath:  image path
     * @param string   $strImage:  image name
     * @param string   $strFormat:  char (4) image format     
     * @param integer  $intWidth:  image width
     * @param integer  $intHeight:  image height
	 * @return integer
     *    0: INSERT SUCCESS
     *    1: ERROR - NOT PERMISSION UPDATE
     *      	
	 */  
	public function add($intLoginId,$intClubId, $intSrcId, $intType, $strName, $strTag, $strShort,$strDescription, $strPath, $strImage, $cFormat, $dblWidth, $dblHeight ) {
		$stmt = $this->database->prepare('CALL clubs_img_add(?,?,?,?,?,?,?,?,?,?,?,?,?)');
		$stmt->bindParam(1, $intLoginId,       PDO::PARAM_INT, 11);
        $stmt->bindParam(2, $intClubId,       PDO::PARAM_INT, 11);		
        $stmt->bindParam(3, $intSrcId,         PDO::PARAM_INT, 11);
		$stmt->bindParam(4, $intType,          PDO::PARAM_INT, 4);
		$stmt->bindParam(5, $strName,          PDO::PARAM_STR, 100);
		$stmt->bindParam(6, $strTag,           PDO::PARAM_STR, 150);
		$stmt->bindParam(7, $strShort,         PDO::PARAM_INT, 255);		
		$stmt->bindParam(8, $strDescription,   PDO::PARAM_STR, 255);
        $stmt->bindParam(9, $strPath,          PDO::PARAM_STR, 150);
        $stmt->bindParam(10,$strImage,          PDO::PARAM_STR, 150);
        $stmt->bindParam(11,$cFormat,          PDO::PARAM_STR, 4);
        $stmt->bindParam(12,$dblWidth,         PDO::PARAM_STR);
        $stmt->bindParam(13,$dblHeight,        PDO::PARAM_STR);		
		$stmt->execute();
		$result = $stmt->fetch();
		return isset($result['errCode']) ? $result['errCode'] : -1;	
	}
	/**
     * @author: Pham Van Diep
	 * @description: update club image info  
	 * @param interger $intLoginId: id admin loggin (as root id width insert permission)
     * @param integer  $intId: image ID
	 * @param integer  $intSrcId: referal id
	 * @param integer  $intType: image type
	 * @param string   $strName: name
	 * @param string   $strTag:  tag
     * @param string   $strShort:  short description
     * @param string   $strDescription:  image description
     * @param string   $strPath:  image path
     * @param string   $strImage:  image name
     * @param string   $strFormat:  char (4) image format     
     * @param integer  $intWidth:  image width
     * @param integer  $intHeight:  image height
	 * @return integer
     *    0: INSERT SUCCESS
     *    1: ERROR - NOT PERMISSION UPDATE
     *      	
	 */ 
          
	public function update($intLoginId, $intId, $intClubId,$intSrcId, $intType, $strName, $strTag, $strShort,$strDescription, $strPath, $strImage, $cFormat, $dblWidth, $dblHeight ) {
		$stmt = $this->database->prepare('CALL clubs_img_update(?,?,?,?,?,?,?,?,?,?,?,?,?,?)');
		$stmt->bindParam(1, $intLoginId,       PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $intId,            PDO::PARAM_INT, 11);
        $stmt->bindParam(3, $intClubId,        PDO::PARAM_INT, 11);
        $stmt->bindParam(4, $intSrcId,         PDO::PARAM_INT, 11);
		$stmt->bindParam(5, $intType,          PDO::PARAM_INT, 4);
		$stmt->bindParam(6, $strName,          PDO::PARAM_STR, 100);
		$stmt->bindParam(7, $strTag,           PDO::PARAM_STR, 150);
		$stmt->bindParam(8, $strShort,         PDO::PARAM_INT, 255);		
		$stmt->bindParam(9, $strDescription,   PDO::PARAM_STR, 255);
        $stmt->bindParam(10, $strPath,         PDO::PARAM_STR, 150);
        $stmt->bindParam(11,$strImage,         PDO::PARAM_STR, 150);
        $stmt->bindParam(12,$cFormat,          PDO::PARAM_STR, 4);
        $stmt->bindParam(13,$dblWidth,         PDO::PARAM_STR);
        $stmt->bindParam(14,$dblHeight,        PDO::PARAM_STR);		
		$stmt->execute();        
		$result = $stmt->fetch();
		return isset($result['errCode']) ? $result['errCode'] : -1;	
	}
    /**
    * @author: Pham Van Diep    
    * @description: update image status 
    * @param integer $intLoginid: user login id
    * @param integer $intClubid: club id
    * @param integer $intStatus: club status
    * @param integer $intPerm: permission      
    * @param integer
    */
    public function updateStatus($intLoginid,$intClubid,$intStatus,$intPerm){
        $stmt = $this->database->prepare('CALL clubs_img_update_status(?,?,?,?)');
		$stmt->bindParam(1, $intLoginid,  PDO::PARAM_INT, 11);
        $stmt->bindParam(2, $intClubid,  PDO::PARAM_INT, 11);
		$stmt->bindParam(3, $intStatus, PDO::PARAM_INT, 4);
		$stmt->bindParam(4, $intPerm, PDO::PARAM_INT, 4);							
		$stmt->execute();	
        $result = $stmt->fetch();
        return isset($result['errCode']) ? $result['errCode'] : -1;	
    }	

    /**
    * @author: Pham Van Diep    
    * @description: delete club
    * @param integer $intLoginid: user login id
    * @param integer $intClubid: club id     
    * @param integer
    */
    public function delete($intLoginid,$intClubid){
        $stmt = $this->database->prepare('CALL clubs_img_delete(?,?)');
		$stmt->bindParam(1, $intLoginid, PDO::PARAM_INT, 11);
        $stmt->bindParam(2, $intClubid,  PDO::PARAM_INT, 11);
		$stmt->execute();
        $result = $stmt->fetch();
        return isset($result['errCode']) ? $result['errCode'] : -1;			
    }
   	/**
	*	@description: get Error code from query result
	*	@param: mixed query result
	*	@return: int
	*/
	public function getErrorCode ( $tableData) {
		return isset($tableData['errCode'])?intval($tableData['errCode']):0;
	}	
	
}